
$(function(){
	$("select").each(function(){
		$(this).append("<option value=''>---请选择---</option>");
	});

	var basePath = $("#basePath").val();
	$.ajax({
		url: basePath + "base_info/ajax_un_pigeonhole_base_data",
		type: "GET",
		dataType: "json",
		success: function( datas ) {
			console.log(datas);
			init(datas);
			
		}
	});
	
	
});

function init(baseInfo){
	var medicalDepts = baseInfo['medicalDepts'];
	$("select[name='outHospitalDeptCode'],[name='outDeptCode']").each(function(){
		var options = "";
		for(var index in medicalDepts){
			var medicalDept = medicalDepts[index];
	    	options+="<option value='"+medicalDept.uniq_code+"'>"+medicalDept.label+"</option>"
	    } 
       $(this).append(options);
       
	});
}

/*点击查询按钮*/
function queryBtnClick(){
	$("#page_index_check").html("");
	$("input[name='currentPage']").val(1);
	$("#queryForm").removeAttr("action",null);
	queryFormSubmit();
}

/*提交查询*/
function queryFormSubmit(){
	var submitData = $('#queryForm').serialize();
	var basePath = $("#basePath").val();
	layer.load(1,{offset: '300px'});
	
	clearPage();
	$.ajax({
		url: basePath + "medical_record_check/ajax_page_index_check",
		dataType: "json",
		data:submitData,
		success: function( data ) {
			var success = data['success'];
			if(success){
				
				var medicalRecords = data['data'];
				if(medicalRecords.length > 0){
					$("#page_index_check").html("<input type='button' value='导出' onclick='exportToExcel()'/>");
					
					
					layer.msg("审核成功",{offset: '300px'});
					addRows(medicalRecords);
				}else{
					layer.msg("审核结果为空",{offset: '300px'});
				}
				
			}else{
				layer.msg("审核失败",{offset: '300px'});
			}
			console.log(data);
			
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			if(XMLHttpRequest.status == 200){
				if(textStatus == 'parsererror'){
					layer.msg("登陆已过期，请重新登陆",{offset: '300px'});
				}else{
					layer.msg("未知错误，请联系管理员",{offset: '300px'});
				}
			}else if(XMLHttpRequest.status == 0){
				if(textStatus == 'error'){
					layer.msg("无法连接服务器，请检测网络",{offset: '300px'});
				}else{
					layer.msg("未知错误，请联系管理员",{offset: '300px'});
				}
			}else{
				layer.msg("未知错误，请联系管理员",{offset: '300px'});
			}
		},
		complete:function(XMLHttpRequest, textStatus){
			layer.closeAll('loading');
			parent.document.getElementById("mainContent").height=document.body.scrollHeight;
		}
	});
	
};

/*添加列表行*/
function addRows(medicalRecords){
	for(var index in medicalRecords){
		var record = medicalRecords[index];
		var add_content=$("#template_tr").clone();
		add_content.removeAttr("hidden");
		add_content.attr("id",record.visit_guid);
		add_content.find("span[class='only_id']").html(record.only_id);
		add_content.find("span[class='mr_id']").html(record.mr_id);
		add_content.find("span[class='visit_number']").html(record.visit_number);
		add_content.find("span[class='patient_name']").html(record.patient_name);
		/*add_content.find("span[class='id_number']").html(record.id_number);*/
		add_content.find("span[class='out_dept_name']").html(record.out_dept_name);
		add_content.find("span[class='out_hospital_date']").html(record.out_hospital_date_format);
		add_content.find("span[class='out_hospital_type_name']").html(record.out_hospital_type_name);
		add_content.find("span[class='quality_control_status_name']").html(record.quality_control_status_name);
		add_content.find("span[class='coding_status_name']").html(record.coding_status_name);
		add_content.find("span[class='error_desc']").html(record.error_desc).attr("title",record.error_desc);
		var check_descs = record.check_desc;
		var addTrs = "<table style='width:450px; margin-left: 4px; border-collapse:collapse;table-layout: fixed;'>" +
				"<thead><tr><td class='tdLabel_4' style='width: 450px;'>审核详情</td></tr></thead>";
		for(var index in check_descs){
			var check_desc = check_descs[index];
			addTrs+="<tr><td class='tdLabel_5' style='width: 450px;'><span id='cust_hidden' title='"+check_desc+"'>"+check_desc+"</span></td></td>"
	    }
		addTrs+="<tbody></tbody></table>";
		add_content.find("div[class='check_desc']").append(addTrs);
		$("#query_show_table tbody").first().append(add_content);
		
    } 
	
}

/*清空列表和分页控件*/
function clearPage(){
	$("#template_tr").siblings("tr").remove();
}

function showDetail(obj){
	/*var content = $(obj).parent();
	layer.open({
		  type: 1,
		  title:'审核详情',
		  offset:'100px',
		  skin: 'layui-layer-rim', //加上边框
		  area: ['470px', '350px'], //宽高
		  content: content.find(".check_desc").html()
	});*/
	var basePath = $("#basePath").val();
	 var content = $(obj).parent().parent();
	 var visitGuid = content.attr("id");
	window.open(basePath+'medical_record_coding/edit_index_info?visitGuid='+visitGuid);
}

function exportToExcel(){
	var basePath = $("#basePath").val();
	$("#queryForm").attr("action",basePath+"excel/page_index_check");
	$("#queryForm").submit();
}
